templateViewInit = ()=>{
    searchFormInfo();
    // 已知数据渲染
    var inst = table.render({
        elem: '#task-list',
        id:'task-list-id',
        height: '#content-body-300',
        url: top.tools.getBaseUrl()+'/scheduled/task/getPage',
        method: 'post',
        headers:{
            'Access-Token':top.tools.cookie.token(),
            'Content-Type':'application/json',
        },
        toolbar: '#dictToolbar',
        defaultToolbar: [],
        cols: [
            [
                {type: 'checkbox', width: 50},
                {type: 'numbers', title: '序号', align: 'center', width: 80},
                {field: 'name', title: '任务名称', align: 'left', minWidth: 200 },
                {field: 'cronExpression', title: 'Cron表达式', align: 'left',width: 110},
                {field: 'className', title: 'Bean ID', align: 'center',width: 200},
                {field: 'status', title: '任务状态', align: 'left',width: 100,templet:function (d) {
                        let c = 'notify-msg-'+d.status.toLocaleLowerCase();
                        let b = d.status.toLocaleLowerCase()=='error' ? '异常':(d.status.toLocaleLowerCase()=='stopped'?'停止':'运行中');
                        return '<span class="notify-msg '+c+'" style="padding: 0 20px;">'+b+'</span>';
                    }},
                {field: 'enabled', title: '是否启用', align: 'left',width: 100,templet:function (d){
                        return '<input type="checkbox" name="enabled" id="'+d.id+'" value="'+d.enabled+'" ' +
                            'title="启用|停用" lay-skin="switch" lay-filter="task-enabled" '+( d.enabled ? "checked" : "" )+'>';
                    }},
                {field: 'lastExecuteTime', title: '最后执行时间', align: 'left',width: 160,templet:function (d){
                        return top.tools.form.formatDate(d.lastExecuteTime);
                    }},
                {field: '', title: '执行记录', align: 'center',width: 100,templet:"<a href=\"javascript:;\" class='a-name' onclick=\"openLogsList('{{= d.id }}')\">点击</a>"},
                {field: 'createTime', title: '创建时间', align: 'center',width: 160,templet:function (d){
                        return top.tools.form.formatDate(d.createTime);
                    }},
                {fixed: 'right', title: '操作', align: 'center',  minWidth: 200, templet: '#barDemo'}
            ]
        ],
        parseData: dataToFormat,
        data:[
            {id:1,name:'用户性别',type:'system_user_sex',status:'0',remark:'',createTime:'2021-01-05 17:03:48'}
        ],
        //skin: 'line', // 表格风格
        //even: true,
        page: true, // 是否显示分页
        limits: [15, 20, 30,50],
        limit: 15, // 每页默认显示的数量
        done: function (res, curr, count) {
            $(".layui-table th[data-field=" + 0 + "] .layui-table-cell").html("");
        }
    });
    // 工具栏事件
    table.on('toolbar(task-list)', function (obj) {
        var id = obj.config.id;
        var checkRow = table.checkStatus(id);
        var data = checkRow.data;
        if (obj.event === 'add') {
            openForm({
                title:"任务配置",
                btn:1,
                save:(closeid)=>{
                    formFun.save(inst,closeid)
                },
                reset:(closeid)=>{
                    form.val('task_form', top.tools.form.getFindElementOnObj("#lay-task-form",false));
                },
                before:(closeid)=>{
                    cron.render({
                        elem: "#cron",                  // 绑定元素
                        run:'/scheduled/getCron',
                        done: function(cronStr) {       // 点击确定，触发，参数为 cron 表达式字符串
                            console.log(cronStr);
                        }
                    });
                },
                area:'40%'
            },'task_form')
        } else if (obj.event === 'view') {
            if (checkRow.data.length<=0){
                layer.msg("请选择数据！");
                return
            }
            openForm({
                title:"任务配置",
                btn:-1,
                area:'40%',
                before:(closeid)=>{
                    form.val('task_form', data);
                }
            },'task_form')
        } else  if (obj.event === 'del') {
            if (checkRow.data.length<=0){
                layer.msg("请选择数据！");
                return
            }
            let ids = data.map((dict) => dict.id);
            openConfirm(()=>{
                formFun.remove({
                    id:ids.join(',')
                },inst)
            });
        }
    });
    // 触发单元格工具事件
    table.on('tool(task-list)', function (obj) { // 双击 toolDouble
        var data = obj.data; // 获得当前行数据
        if (obj.event === 'edit') {
            openForm({
                title:"任务配置",
                btn:1,
                save:(closeid)=>{
                    formFun.save(inst,closeid)
                },
                reset:(closeid)=>{
                    form.val('dict_form', top.tools.form.getFindElementOnObj("#lay-task-form",false));
                },
                area:'40%',
                before:(closeid)=>{
                    formFun.setValue(data,'task_form')
                    cron.render({
                        elem: "#cron",                  // 绑定元素
                        run:'/scheduled/getCron',
                        done: function(cronStr) {       // 点击确定，触发，参数为 cron 表达式字符串
                            console.log(cronStr);
                        }
                    });
                }
            },'task_form')
        } else if (obj.event === 'view') {
            openForm({
                title:"任务配置",
                btn:-1,
                area:'40%',
                before:(closeid)=>{
                    formFun.setValue(data,'task_form')
                }
            },'task_form')
        } else if (obj.event === 'del') {
            openConfirm(()=>{
                formFun.remove(data,inst)
            });
        } else if (obj.event === 'run' || obj.event === 'stop') {
            openConfirm(()=>{
                formFun.execute(data,inst)
            },undefined,'确定要运行吗？');
        } else if (obj.event === 'log') {
            formFun.getLog(data)
        }
    });


    queryButton('task-list-id');
    formReset();
    // 状态 - 开关操作
    form.on('switch(task-enabled)', function(obj){
        let data = {
            id:$(obj.elem).attr("id"),
            enabled:obj.elem.checked? 1:0
        }
        top.API.task().save(data,()=>{
            table.reloadData('task-list-id', {
                where: {}, // 数据异步请求时携带的字段集 --- 属性设置有效，因属于数据相关属性
                scrollPos: true, // 设定重载数据或切换分页时的滚动条的位置状态 --- 属性设置有效
                page: {
                    curr: inst.config.page.curr,
                    limit: inst.config.page.limit
                }
            });
        })
    });

    top.API.task().countStatus({},(res)=>{
        var run = document.querySelector("#run");
        var stop = document.querySelector("#stop");
        var error = document.querySelector("#error");
        run.setAttribute('data-num',res.data.RUNNING)
        $("#run").numScroll();
        stop.setAttribute('data-num',res.data.STOPPED)
        $("#stop").numScroll();
        error.setAttribute('data-num',res.data.ERROR)
        $("#error").numScroll();
    })
}

var formFun = {
    save:(typeTable,closeid)=>{
        let formData = top.tools.form.getFindElementOnObj("#lay-task-form",true);
        if (typeof formData === 'boolean'){
            return;
        }
        if (formData.enabled && formData.enabled === 'on' ){
            formData.enabled = 1;
        }else {
            formData.enabled = 0;
        }

        top.API.task().save(formData,(res)=>{
            layer.msg(res.message, {icon: 1}, function(){
                // layer.msg('提示框关闭后的回调');
                if (typeTable){
                    table.reloadData('task-list-id', {
                        where: {}, // 数据异步请求时携带的字段集 --- 属性设置有效，因属于数据相关属性
                        scrollPos: true, // 设定重载数据或切换分页时的滚动条的位置状态 --- 属性设置有效
                        page: {
                            curr: typeTable.config.page.curr,
                            limit: typeTable.config.page.limit
                        }
                    });
                }
                if (closeid){
                    layer.close(closeid);
                }
            });
        })
    },
    remove:(data,typeTable)=>{
        top.API.task().del(data,(res)=>{
            layer.msg(res.message, {icon: 1}, function(){
                // layer.msg('提示框关闭后的回调');
                if (typeTable){
                    table.reloadData('task-list-id', {
                        where: {}, // 数据异步请求时携带的字段集 --- 属性设置有效，因属于数据相关属性
                        scrollPos: true, // 设定重载数据或切换分页时的滚动条的位置状态 --- 属性设置有效
                        page: {
                            curr: typeTable.config.page.curr,
                            limit: typeTable.config.page.limit
                        }
                    });
                }
            });
        })
    },
    setValue:(data,eleId)=>{
        data.enabled = parseInt(data.enabled);
        form.val(eleId, data);
    },
    execute:(data,typeTable)=>{
        top.API.task().execute({execute:'run',id:data.id},(res)=>{
            layer.msg(res.message, {icon: 1}, function(){
                // layer.msg('提示框关闭后的回调');
                if (typeTable){
                    table.reloadData('task-list-id', {
                        where: {}, // 数据异步请求时携带的字段集 --- 属性设置有效，因属于数据相关属性
                        scrollPos: true, // 设定重载数据或切换分页时的滚动条的位置状态 --- 属性设置有效
                        page: {
                            curr: typeTable.config.page.curr,
                            limit: typeTable.config.page.limit
                        }
                    });
                }
            });
        })
    },
    getLog:(data)=>{
        top.API.task().log(data,(res)=>{
            res.data.startTime = top.tools.form.formatDate(res.data.startTime);
            res.data.endTime = top.tools.form.formatDate(res.data.endTime);
            openForm({
                title:"执行日志详情",
                btn:-1,
                data:res.data,
                area:'60%',
                before:(closeid)=>{
                    // code
                    layui.code({
                        elem: '.code-demo'
                    });
                }
            },'log_info')
        })
    },
}

function openLogsList(id){
    top.API.task().logs({id:id},(res)=>{
        layer.open({
            title: '任务执行日志',
            type: 1,
            offset: 'r',
            anim: 'slideLeft', // 从右往左
            area: ['50%', '100%'],
            shade: 0.1,
            shadeClose: true,
            move:false,
            id: 'ID-demo-layer-direction-r',
            content: '<div style="padding: 16px;"><table class="layui-hide" id="logs-list" lay-filter="logs-list"></table></div>',
            success:function (){
                // 已知数据渲染
                var dictDataTable = table.render({
                    elem: '#logs-list',
                    id:"logs-list-id",
                    cols: [
                        [
                            {type: 'numbers', title: '序号', align: 'center', width: 50},
                            {field: 'taskName', title: '任务名称', align: 'left', minWidth: 150},
                            {field: 'startTime', title: '开始时间', align: 'center',width: 180,templet: function(d){
                                let time = top.tools.form.formatDate(d.startTime);
                                return time;
                                }},
                            {field: 'endTime', title: '结束时间', align: 'center',width: 180,templet: function(d){
                                    let time = top.tools.form.formatDate(d.endTime);
                                    return time;
                                }},
                            {field: 'status', title: '执行状态', align: 'center',width: 100},
                            {fixed: 'right', title: '操作', align: 'center',  width: 80, templet: function(d){
                                    return " <a class=\"layui-btn layui-btn-xs layui-btn-link\" lay-event=\"view\" >查看</a>";
                                }}
                        ]
                    ],
                    data:res.data || [
                        {id:'1',sort:'1',label:'男',value:'1',dict_type:'system_user_sex',status:'0',colorType:'default',cssClass:'default',remark:'性别男',createTime:'2021-01-05 17:03:48'},
                        {id:'2',sort:'2',label:'女',value:'2',dict_type:'system_user_sex',status:'0',colorType:'default',cssClass:'default',remark:'性别男',createTime:'2021-01-05 17:03:48'}
                    ],
                    done: function (res, curr, count) {
                        $(".layui-table th[data-field=" + 0 + "] .layui-table-cell").html("");
                    }
                });


                // 触发单元格工具事件
                table.on('tool(logs-list)', function (obj) { // 双击 toolDouble
                    var data = obj.data; // 获得当前行数据
                    if (obj.event === 'view') {
                        data.startTime = top.tools.form.formatDate(data.startTime);
                        data.endTime = top.tools.form.formatDate(data.endTime);
                        openForm({
                            title:"执行日志详情",
                            btn:-1,
                            data:data,
                            area:'60%',
                            before:(closeid)=>{
                                // code
                                layui.code({
                                    elem: '.code-demo'
                                });
                            }
                        },'log_info')
                    }
                });
            }
        });
    })
}